using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using CocaCola_Inventory_System.Data_Access;
namespace CocaCola_Inventory_System.Forms
{
    public partial class Container : Form
    {
        public OdbcConnection con;
        public int security = -1;
        public int id = -1;
        public string Accountname="";
        public string server = "";
        public string email = "";
        public int stockid = 0;

        public Container()
        {
            Switchboard.showSplash();
            InitializeComponent();
        }

        private void dataInsertionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security < 2)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            Form1 f = new Form1();
            f.MdiParent = this;
            f.Show();
        }

        private void transactionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security < 1)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            search s = new search();
            s.MdiParent = this;
            s.Show();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            MessageBox.Show(" KO IT Department Inventory System  \n \n Done by \n Kamal Haggag , \n Mahmoud Saleh , \n Yasser Zayan ", "About");
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security > 0 && Accountname != "" && id > 0)
            {
                retrieve r = new retrieve(con);
                r.logout(id);

                security = -1;
                id = -1;
                Accountname = "";
                refresh();
                foreach (Form f in this.MdiChildren)
                    f.Dispose();

            }
            Application.Exit();
        }

        private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }

        private void arrangeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void arrangeToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void verticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        private void horizontallyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void arrangeToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void Container_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (security > 0 && Accountname != "" && id > 0)
            {
                retrieve r = new retrieve(con);
                r.logout(id);

                security = -1;
                id = -1;
                Accountname = "";
                refresh();
            }
            Application.Exit();
        }

        private void replicateUsersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }

            DialogResult res = MessageBox.Show("Confirm Replicating Users ??","Confirmation",MessageBoxButtons.YesNo);
            if (res != DialogResult.Yes)
                return;
            Replicate r = new Replicate();
            r.MdiParent = this;
            r.Show();
        }

        private void Container_Load(object sender, EventArgs e)
        {
            try
            {

                
               
                    con = new OdbcConnection(Registery.GetConnectionString());
                    con.StateChange += new StateChangeEventHandler(con_StateChange);
                    stockid = Common.GetStockUserId(con);
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Loading Settings","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                Application.Exit();
            }

            Login l = new Login();
            l.MdiParent = this;

            Switchboard.closeSplash();
            this.WindowState = FormWindowState.Maximized;
            this.ShowInTaskbar = true;
            l.Show();           
            
        }

        void con_StateChange(object sender, StateChangeEventArgs e)
        {
            if (e.CurrentState == ConnectionState.Open)
                this.Text = "Coca Cola Inventory System 1.0 ";
            else if(e.CurrentState == ConnectionState.Closed||e.CurrentState==ConnectionState.Broken)
                this.Text = "Coca Cola Inventory System 1.0 [Offline]";


        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form f in this.MdiChildren)
            {
                if (f.GetType() == typeof(Login))
                    return;
            }
            if (security > 0 && Accountname != ""&& id >0)
            {
                retrieve r = new retrieve(con);
                if (r.logout(id))
                    MessageBox.Show("You Have Logged Out Successfully"); 
                else
                    MessageBox.Show("Error During LogOut");

                security = -1;
                id = -1;
                Accountname = "";
                refresh();
                foreach (Form f in this.MdiChildren)
                    f.Dispose();

            }
            else
            {
                Login l = new Login();
                l.Show();
                l.MdiParent = this;
            }

        }

        public void refresh()
        {
            if (security > 0 && Accountname != "")
                loginToolStripMenuItem.Text = Accountname + ", Log Out";
            else
                loginToolStripMenuItem.Text = "Login";
            if (security == 3)
                administratorToolStripMenuItem.Visible = true;
            else
                administratorToolStripMenuItem.Visible = false;
            
            if (security == 3)
                replicateUsersToolStripMenuItem.Visible = true;
            else
                replicateUsersToolStripMenuItem.Visible = false;
        }

        private void manageUsersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security != 3)
                return;
            ManageUsers u = new ManageUsers();
            u.MdiParent = this;
            u.Show();

        }

        private void viewLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security != 3)
                return;
            Log l = new Log();
            l.MdiParent = this;
            l.Show();
        }

        private void deleteLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security != 3)
                return;
            DialogResult res = MessageBox.Show("Confirm Deleting Log", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (res != DialogResult.Yes)
                return;
            
            Insert i = new Insert(con);
            i.deletelog(id);

            

        }

        private void configureSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void configureSettingsToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Setting s = new Setting();
            s.MdiParent = this;
            s.Show();
        }

        private void bUToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            BU b = new BU();
            b.MdiParent = this;
            b.Show();
        }

        private void countriesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            Countries c = new Countries();
            c.MdiParent = this;
            c.Show();
        }

        private void locationsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            Locations l = new Locations();
            l.MdiParent = this;
            l.Show();
        }

        public void showDefaultWindow()
        {
            if (security < 1)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            search s = new search();
            s.MdiParent = this;
            s.Show();

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            Detail_types dt = new Detail_types();
            dt.MdiParent = this;
            dt.Show();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            conditionsManager ty = new conditionsManager();
            ty.MdiParent = this;
            ty.Show();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            Categories c = new Categories();
            c.MdiParent = this;
            c.Show();
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            deprec dep = new deprec();
            dep.MdiParent = this;
            dep.Show();
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            Types ty = new Types();
            ty.MdiParent = this;
            ty.Show();
        }

        private void usersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            userMan ty = new userMan();
            ty.MdiParent = this;
            ty.Show();
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            categoryManager ty = new categoryManager();
            ty.MdiParent = this;
            ty.Show();
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            depresionationMan ty = new depresionationMan();
            ty.MdiParent = this;
            ty.Show();
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            typeManager ty = new typeManager();
            ty.MdiParent = this;
            ty.Show();
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            detailManager ty = new detailManager();
            ty.MdiParent = this;
            ty.Show();
        }

        private void configureStockUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            StockUser l = new StockUser();
            l.MdiParent = this;
            l.Show();
        }

        private void configureMailSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            EmailConfig l = new EmailConfig();
            l.MdiParent = this;
            l.Show();
        }
    }
}